﻿using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using UnityEngine.UI;

public class BarChart : MonoBehaviour {

    public int Max;
    public int Min;
    public int Categories;
    public List<GameObject> Bars;
    public RectTransform rectTransform;
    private float height;
    private bool startAnimation;
    int count = 0;
    private bool creationComplete;

    void Awake()
    {
        float mid = ((Max - Min) / 2);
        //GameObject.Find("Mid").GetComponent<Text>().text = mid.ToString();
        //GameObject.Find("Max").GetComponent<Text>().text = Max.ToString();
        //GameObject.Find("Min").GetComponent<Text>().text = Min.ToString();
        Bars = new List<GameObject>();
        rectTransform = this.GetComponent<RectTransform>();
        height = 800;
    }
    // Use this for initialization
    void Start () {

        CreateBars();
    }
	
	// Update is called once per frame
	void Update () {

        if (creationComplete)
        {
            if (Bars[Categories - 1].GetComponent<Bar>().isDoneAnimating)
            {
                startAnimation = false;
            }
            if (startAnimation == true)
            {

                if (count < Categories)
                {
                    if (Bars[count].GetComponent<Bar>().isAnimating == false && Bars[count].GetComponent<Bar>().isDoneAnimating == false)
                    {
                        Bars[count].GetComponent<Bar>().startTime = Time.time;
                        Bars[count].GetComponent<Bar>().Animate();

                    }
                    else if (Bars[count].GetComponent<Bar>().isDoneAnimating)
                    {
                        count++;

                    }
                }
            }
        }
    }


    public void Reset()
    {

        count = 0;
        creationComplete = false;
        foreach(GameObject bar in Bars)
        {
            Destroy(bar);
        }
        Bars.Clear();
    }

    public void UpdateMax()
    {
        float mid = ((Max - Min) / 2);
        GameObject.Find("Mid").GetComponent<Text>().text = mid.ToString();
        GameObject.Find("Max").GetComponent<Text>().text = Max.ToString();
        GameObject.Find("Min").GetComponent<Text>().text = Min.ToString();
    }


    public void CreateBars()
    {
        float mid = ((Max - Min) / 2);
        //GameObject.Find("Mid").GetComponent<Text>().text = mid.ToString();
        //GameObject.Find("Max").GetComponent<Text>().text = Max.ToString();
        //GameObject.Find("Min").GetComponent<Text>().text = Min.ToString();
        creationComplete = true;

        float barHeight = height / Categories;

        int count = 0;
        while (count < Categories)
        {
            Bars.Add(GameObject.Instantiate(Resources.Load<GameObject>("Bar")));
            count++;
        }

        count = 0;
        foreach(GameObject bar in Bars)
        {
            bar.GetComponent<RectTransform>().parent = GameObject.Find("BarChart").transform;
            bar.GetComponent<Bar>().GetComponent<RectTransform>().localPosition = Vector3.zero;
            //bar.GetComponent<Bar>().color = new Color(Random.Range(0f,1f), Random.Range(0f, 1f), Random.Range(0f, 1f),1);
            bar.GetComponent<Bar>().value = Random.Range(Min, Max);
            bar.GetComponent<Bar>().Height = barHeight;
            bar.GetComponent<Bar>().GetComponent<RectTransform>().localPosition = new Vector3 (0, (count * barHeight), 0); 
            bar.GetComponent<Bar>().Title = "Test Bar";
            count++;
        }
    }


    public void AnimateBars()
    {
        startAnimation = true;       
    }
}
